﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("SkinnedMeshRenderer/Bounds/Reset",
            "Sets the Bounds of a Skinned Mesh Renderer to match it's mesh", 0)]
        public static void SkinnedMeshRendererSetBounds()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<SkinnedMeshRenderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                SkinnContext.UndoRecord(context, "Reset Bounds");
                context.SetBounds();
            }

            SkinnContext.Finished();
        }
       

        [CtxMenuItem("SkinnedMeshRenderer/Bounds/Reset All",
          "Sets the bounds of all Skinned Mesh Renderers under the same root.", ContextRequirements.SingleItem, 0)]
        public static void SkinnedMeshRendererSetBoundsAll()
        {
            var roots = new Dictionary<int, Transform>();
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<SkinnedMeshRenderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                var id = context.transform.root.gameObject.GetInstanceID();
                if (!roots.ContainsKey(id)) roots.Add(id, context.transform.root);
            }
            foreach (var item in roots)
            {
                var smrs = item.Value.gameObject.GetValidSkinnedMeshes();
                for (int i = 0; i < smrs.Count; i++) { Undo.RecordObject(smrs[i], "Reset Bounds"); smrs[i].SetBounds(); }
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("SkinnedMeshRenderer/Bounds/Encapsulate/Selection",
          "Encapsulates the bounds of the selected Skinned Mesh Renderer(s).", 0)]
        public static void SkinnedMeshRendererEncapsulateBounds()
        {

            var skinnedMeshes = new List<SkinnedMeshRenderer>();
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<SkinnedMeshRenderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;

                skinnedMeshes.Add(context);

                SkinnContext.UndoRecord(context, "Encapsulate Bounds");
            }
            skinnedMeshes.ToArray().SetBounds(true);
            SkinnContext.Finished();
        }

        [CtxMenuItem("SkinnedMeshRenderer/Bounds/Encapsulate/All",
         "Encapsulates the bounds of the all the Skinned Mesh Renderers under the same root.", ContextRequirements.SingleItem, 0)]
        public static void SkinnedMeshRendererEncapsulateBoundsAll()
        {
            var item = SkinnContext.GetContextItem();
            if (SkinnEx.IsNullOrNotInAScene(item.transform)) { SkinnContext.Finished(); return; }
            var skinnedMeshes = item.GetValidSkinnedMeshes(true, false);
            foreach (var skinnedMesh in skinnedMeshes) SkinnContext.UndoRecord(skinnedMesh, "Encapsulate All");
            skinnedMeshes.ToArray().SetBounds(true);
            SkinnContext.Finished();
        }
    }
}